package com.atguigu.singleton.type4;

/**
 * @ClassName SingleTonTest03
 * @Description TODO
 * @Author HongJian-Yang
 * @Date 2020/6/16 22:54
 * @Version 1.0
 */
public class SingleTonTest04 {
    public static void main(String[] args) {
        SingleTon instance = SingleTon.getInstance();
        System.out.println(SingleTon.getInstance() == instance);
    }
}

class SingleTon {
    private static SingleTon instance;

    private SingleTon() {

    }

    /**
     * 懒汉式,在使用的时候才创建对象,但是这是线程不安全的
     * 使用synchronized,解决线程安全问题,但是synchronized效率不高
     *
     * @return 单例
     */
    public static synchronized SingleTon getInstance() {
        if (instance == null) {
            instance = new SingleTon();
        }
        return instance;
    }
}
